/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.sms.impl;

import com.jianzhou.sdk.BusinessService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.vacoor.nothing.web.sms.SmsSender;

/**
 * 基于建周短信平台的 SMS 服务实现
 * http://www.jianzhou.sh.cn/
 *
 * @author vacoor
 */
// @Service
public class JzSmsSenderImpl implements SmsSender, InitializingBean {
    private static final String MOBILE_SEPARATOR = ";";
    private BusinessService smsService;
    private String wsdlUrl;
    private String username;
    private String password;
    private String signature;  // 签名, 每条信息后必须加

    @Override
    public void sendSms(String mobile, String message) {
        Assert.hasText(mobile);

        message += signature;
        // > 0 都是成功, 返回 task id
        /*return -1 < */smsService.sendMessage(username, password, mobile, message);
    }

    @Override
    public void sendSms(String[] mobiles, String message) {
        Assert.notEmpty(mobiles);
        if (mobiles == null || mobiles.length < 1) {
//            return false;
        }

        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < mobiles.length; i++) {
            if (i > 0) {
                buffer.append(MOBILE_SEPARATOR);
            }
            buffer.append(mobiles[i]);
        }

        message += signature;
        /*return -1 < */smsService.sendMessage(username, password, buffer.toString(), message);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(wsdlUrl);
        Assert.hasText(username);
        Assert.hasText(password);
        Assert.hasText(signature);

        smsService = new BusinessService(wsdlUrl);
    }

    /* ******************
     *  getter / setter
     * ******************/

    public String getWsdlUrl() {
        return wsdlUrl;
    }

    public void setWsdlUrl(String wsdlUrl) {
        this.wsdlUrl = wsdlUrl;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }
}
